<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>

    <script>
        function foo () {
            alert('hello world');
        }


        // script标签最好写在body里最后
        // 页面上的DOM元素挂载完毕后执行
        window.onload = function () {
            var boxDom2 = document.getElementsByClassName('box2')[0];
            boxDom2.onclick = function () {
                alert('hello, box2');
            } 
        }
       
    </script>
</head>
<body>
    <div class="box" onclick="foo()">div1</div>
    <div class="box box2">div2</div>
    
</body>
</html>